package login;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cgy.entity.Admin;
import com.fasterxml.jackson.core.JsonProcessingException;

import util.TokenUtil;

@RestController 
@CrossOrigin
@RequestMapping("login")

public class loginController {

	/**
	 * 管理员登录	 
	 * @param username
	 * @param password
	 * @return
	 */
	TokenUtil token = new TokenUtil(); 
	@GetMapping
	public Map<String, Object> login(String username,String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		Admin admin = new Admin();
		if(admin.getUname().equals(username) && admin.getUpassword().equals(password)) {
			try {
				String sign = token.sign(admin, 1000*60*60);
				map.put("token", sign);
				map.put("username", admin.getUname());
				map.put("code", 0);
				System.out.println(sign);
				try {
					Admin unsign = token.unsign(sign, Admin.class);
					System.out.println(unsign);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return map;
		}else {
			return map;
		}
	}
}
